import { type App } from "vue";

export const copy = (app: App) => {
  app.directive("copy", {
    beforeMount(el, binding) {
      el.targetContent = binding.value;
      el.addEventListener("click", () => {
        if (!el.targetContent) return "";
        // 创建textarea标签
        const textarea = document.createElement("textarea");
        // 设置相关属性
        textarea.style.position = "fixed";
        textarea.style.top = "-99999px";
        // 把目标内容赋值给它的value属性
        textarea.value = el.targetContent;
        // 插入到页面
        document.body.appendChild(textarea);
        // 调用onselect()方法
        textarea.select();
        // 把目标内容复制进剪贴板, 该API会返回一个Boolean
        document.execCommand("Copy");
        // 移除textarea标签
        document.body.removeChild(textarea);
        return null;
      });
    },
    updated(el, binding) {
      // 实时更新最新的目标内容
      el.targetContent = binding.value;
    }
  });
};
